package com.example.runningman.friend;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.runningman.MainActivity;
import com.example.runningman.R;
import com.example.runningman.bean.User;
import com.example.runningman.user.LoginActivity;
import com.example.runningman.util.GlideBlurTransformation;
import com.example.runningman.util.SPUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.ref.WeakReference;

public class FriendActivity extends AppCompatActivity {
    ImageView head;
    ImageView pic;
    TextView name;
    TextView sex;
    TextView birthday;
    TextView account;
    ImageButton back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend);
        head=findViewById(R.id.pic_bg);
        pic=findViewById(R.id.picd);
        name=findViewById(R.id.name);
        sex=findViewById(R.id.sex);
        birthday=findViewById(R.id.birthday);
        account=findViewById(R.id.account);
        back=findViewById(R.id.back3);
        mHandler=new Mhandler();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(FriendActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dia;
                dia = new Dialog(FriendActivity.this, R.style.edit_AlertDialog_style);
                dia.setContentView(R.layout.activity_start_dialog);
                ImageView imageView = (ImageView) dia.findViewById(R.id.start_img);

                Glide.with(FriendActivity.this).load(user.getPhotoUrl()).into(imageView);
                //选择true的话点击其他地方可以使dialog消失，为false的话不会消失
                dia.setCanceledOnTouchOutside(true); // Sets whether this dialog is
                Window w = dia.getWindow();
                WindowManager.LayoutParams lp = w.getAttributes();
                lp.x = 0;
                lp.y = 40;
                dia.onWindowAttributesChanged(lp);
                dia.show();
                imageView.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dia.dismiss();
                            }
                        });
            }
        });
        Intent intent = getIntent();
        //接收从First_Activity中传输的数据
        String userid = intent.getStringExtra("userid");
        postRequest(userid);

    }
    /**
     * post请求后台
     * @param userid
     */
    private void postRequest(String userid )  {
        //建立请求表单，添加上传服务器的参数
        RequestBody formBody = new FormBody.Builder()
                .add("userid",userid)
                .build();
        //发起请求
        final Request request = new Request.Builder()
                .url("http://120.26.177.58:8080/AndroidServer01/findUserbyid?")
                .post(formBody)
                .build();
        //新建一个线程，用于得到服务器响应的参数
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    //回调
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        //将服务器响应的参数response.body().string())发送到hanlder中，并更新ui
                        mHandler.obtainMessage(1, response.body().string()).sendToTarget();

                    } else {
                        throw new IOException("Unexpected code:" + response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    private static Gson gson=new Gson();
    private Mhandler mHandler;
    User user;
    final OkHttpClient client = new OkHttpClient();
    private class Mhandler extends Handler {

        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                String ReturnMessage = (String) msg.obj;
                Log.i("获取的返回信息","+"+ReturnMessage+"+");
                user= gson.fromJson(ReturnMessage,User.class);
                Glide.with(FriendActivity.this).load(user.getPhotoUrl())
                        .apply(RequestOptions.bitmapTransform(new GlideBlurTransformation(FriendActivity.this)))
                        .into(head);


                Glide.with(FriendActivity.this).load(user.getPhotoUrl())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))
                        .into(pic);
                name.setText("昵称："+user.getUsername());
                String string= "男";
                if("".equals(user.getSex()+"")||user.getSex()==null){

                }else {
                    if(user.getSex()==2){
                        string="女";
                    }
                }
                sex.setText(string);
                birthday.setText("生日："+user.getBirthday());
                account.setText("个人简介："+user.getAccount());
                }
                /***
                 * 在此处可以通过获取到的Msg值来判断
                 * 给出用户提示注册成功 与否，以及判断是否用户名已经存在
                 */

            }
        }

}
